<script>
	import {
		reportOnlineState
	} from '@/api/online/index.js'
	import {
		isLoginFailure
	} from '@/utils/common.js'
	let WebIM = (wx.WebIM = require("./utils/WebIM")["default"]);
	let disp = require("./utils/broadcast");
	let logout = false;
	let emedia = (uni.emedia = require("./emediaSDK/emedia_for_miniProgram"));

	// 极光推送插件
	const jpushModule = uni.requireNativePlugin("JG-JPush")
	emedia.config({
		useUniappPlugin: true
	});

	export default {
		globalData: {
			unReadMessageNum: 0,
			userInfo: null,
			saveFriendList: [],
			saveGroupInvitedList: [],
			isIPX: false, //是否为iphone X
			conn: {
				closed: false,
				curOpenOpt: {},

				open(opt) {
					this.curOpenOpt = opt;
					WebIM.conn.open(opt);
					this.closed = false;
				},

				reopen() {
					if (this.closed) {
						//this.open(this.curOpenOpt);
						WebIM.conn.open(this.curOpenOpt);
						this.closed = false;
					}
				},
			},
			getUserInfo(cb) {
				var me = this;

				if (this.userInfo) {
					typeof cb == "function" && cb(this.userInfo);
				} else {
					// 调用登录接口
					uni.login({
						success() {
							uni.getUserInfo({
								success(res) {
									me.userInfo = res.userInfo;
									typeof cb == "function" && cb(me.userInfo);
								},
							});
						},
					});
				}
			},
			checkIsIPhoneX: function() {
				const me = this;
				uni.getSystemInfo({
					success: function(res) {
						// 根据 model 进行判断
						if (res.model && res.model.search("iPhone X") != -1) {
							me.isIPX = true;
						}
					},
				});
			},
			timer: null
		},
		onShow() {
			this.reportOnlineState()
		},
		onLaunch() {
			this.initJPush()
			this.reportOnlineState()
			this.pollingRequest()
		},

		methods: {
			// 轮询上报
			pollingRequest() {
				this.timer = setInterval(() => {
					this.reportOnlineState();
				}, 30000);
			},
			// 上报在线状态
			reportOnlineState() {
				// 未登录状态下不进行上报
				const pagelist = ['login', 'register']
				const currentPageList = getCurrentPages()
				if (currentPageList.length == 0) return
				const currentPage = currentPageList[currentPageList.length - 1]?.route
				const isNotLoginState = pagelist.some(item => currentPage.includes(item))
				if (isNotLoginState) return
				const user = uni.getStorageSync('user') || {}
				const {
					id = "", username = ""
				} = user;
				const data = {
					uid: id,
					username,
				};
				reportOnlineState(data);
			},
			initJPush() {
				jpushModule.initJPushService();
				jpushModule.setLoggerEnable(true);
				jpushModule.addConnectEventListener(result => {
					let connectEnable = result.connectEnable
					uni.$emit('connectStatusChange', connectEnable)
					
					console.log(result, 'addConnectEventListener')
				});

				jpushModule.addNotificationListener(result => {
					let notificationEventType = result.notificationEventType
					let messageID = result.messageID
					let title = result.title
					let content = result.content
					let extras = result.extras
					const {
						roomName,
						meeting_id
					} = extras

					console.log(result, 'addNotificationListener')

					uni.showModal({
						title: '提示',
						content: title,
						confirmText: '取消',
						cancelText: '确定',
						success({
							confirm,
							cancel
						}) {
							if (confirm) return

							if (isLoginFailure()) {
								uni.reLaunch({
									url: '/pages/login/login'
								})
							} else {
								uni.navigateTo({
									url: `/pages/video/video?roomName=${roomName}&confrId=${meeting_id}`,
								})
							}
						}
					})

				});

				jpushModule.addCustomMessageListener(result => {
					let type = result.type
					let messageType = result.messageType
					let content = result.content
					console.log(result, 'addCustomMessageListener')
				})

				jpushModule.addLocalNotificationListener(result => {
					let messageID = result.messageID
					let title = result.title
					let content = result.content
					let extras = result.extras
					console.log(result, 'addLocalNotificationListener')
				})
				
				jpushModule.getRegistrationID(result => {
					let registerID = result.registerID
					uni.setStorageSync('registerID',registerID)
					console.log(registerID,'registerID')
				})
			}
		},
	};
</script>
<style lang="scss">
	// @import '/wxcomponents/vant/common/index.wxss';
</style>
